//
//  OGTestWindow.m
//  OsteoGest
//
//  Created by Pierre de FIlippis on 6/22/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "OGTestWindow.h"


@implementation OGTestWindow

- (id)init {
    if ( self = [super initWithContentRect:NSMakeRect(0, 0, 50, 21) styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO] ) {
        _testView = [[OGTestView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)]; //[self contentRectForFrameRect:[self frame]]];
        [self setContentView:_testView];        [self setOpaque:NO];  
        [self setHasShadow:YES];
        [self setIgnoresMouseEvents: YES];
    }
    
    return self;
}

- (void)setCellTip:(NSString *)newTip {
    static NSDictionary *attributes = nil;
    if (!attributes)
        attributes = [[NSDictionary alloc] initWithObjectsAndKeys:[NSFont fontWithName:@"LucidaGrande" size:11], NSFontAttributeName, nil];
    
    NSAttributedString * attributedString = [[NSAttributedString alloc] initWithString: newTip
                                                                            attributes: attributes];
    [_cellTipView setAttributedCellTip: attributedString];

    if (attributedString) {
        NSSize stringSize = [attributedString size];
        NSRect frame = [self frame];
        frame.size.height = round(stringSize.height + CELL_TIP_PADDING) + 3;
        // the height is the twice the radius (width) of the endcaps
        frame.size.width = round(stringSize.width + frame.size.height);
        [self setContentRelativeFrame:frame display:NO];
    }    

    [attributedString release];
}

@end

@implementation OGTestView

- (void)drawRect:(NSRect)rect {
	NSString *newTip = @"bla";
    static NSDictionary *attributes = nil;
    if (!attributes)
        attributes = [[NSDictionary alloc] initWithObjectsAndKeys:[NSFont fontWithName:@"LucidaGrande" size:11], NSFontAttributeName, nil];
    
    NSAttributedString * attributedString = [[NSAttributedString alloc] initWithString: newTip
                                                                            attributes: attributes];
    [_cellTipView setAttributedCellTip: attributedString];

    if (attributedString) {
        NSSize stringSize = [attributedString size];
        NSRect frame = [self frame];
        frame.size.height = round(stringSize.height + CELL_TIP_PADDING) + 3;
        // the height is the twice the radius (width) of the endcaps
        frame.size.width = round(stringSize.width + frame.size.height);
        [self setContentRelativeFrame:frame display:NO];
    }    

    [attributedString release];

    NSPoint stringOrigin;
    NSRect bounds, pathRect;
    NSBezierPath *path;
    static NSColor *borderColor = nil;
    float endCapRadius;
    
    // erase previous drawing
    bounds = [self bounds];
    [[NSColor clearColor] set];
    NSRectFill(bounds);
    
    // draw the outline
    bounds = NSInsetRect(bounds, 2, 2);
    if (!borderColor) {
        borderColor = [[NSColor grayColor] retain];
    }
    // draw a gray bubble
    endCapRadius = bounds.size.height/2.0;
    path = [[NSBezierPath alloc] init];
    [path setLineCapStyle:NSRoundLineCapStyle];
    pathRect.origin.x = bounds.origin.x + endCapRadius;
    pathRect.origin.y = bounds.origin.y + bounds.size.height/2.0;
    pathRect.size.width = bounds.size.width - 2 * endCapRadius;
    pathRect.size.height = bounds.size.height - 1;
    pathRect = NSIntegralRect(pathRect);
    [path moveToPoint:pathRect.origin];
    [path relativeLineToPoint:NSMakePoint(pathRect.size.width, 0)];
    [path setLineWidth:pathRect.size.height];
    [borderColor set];
    [path stroke];
    [path release];
    
    // draw an white bubble inset so the gray that's left is just an outline
    path = [[NSBezierPath alloc] init];
    [path setLineCapStyle:NSRoundLineCapStyle];
    pathRect.origin.x += 0.5;
    pathRect.origin.y += 0.3;
    pathRect.size.width -= 0.6;
    pathRect.size.height -= 0.6;
    [path moveToPoint:pathRect.origin];
    [path relativeLineToPoint:NSMakePoint(pathRect.size.width, 0)];
    [path setLineWidth:pathRect.size.height];
    [[NSColor whiteColor] set];
    [path stroke];
    [path release];
    
    // draw the string
    // bounds.size.height/2.0 is the radius (width) of the endcap
    stringOrigin.x = round(bounds.origin.x + bounds.size.height/2.0);
    stringOrigin.y = round(bounds.origin.y + CELL_TIP_PADDING/2.0);
    [[NSColor blackColor] set];
    [_cellTip drawAtPoint:stringOrigin];
}

@end
